博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
快应用-Canvas实现尺子左右滑动的效果
阅读量:3904 次
发布时间:2019-05-23

本文共 3661 字,大约阅读时间需要 12 分钟。

完成效果:

在这里插入图片描述
在web页面中,如果我们要实现左右滑动的效果,只需要在css中写上:
overflow: hidden;
overflow-x: scroll;
就可以达到效果。但是在快应用中,不支持overflow命令。

所以这里用canvas画图来实现:监听手指左右滑动的事件,手指每滑动一次,重新绘制一次尺子 从而给人视觉上滑动的效果。

核心代码:

template

{
{shortDate==today?'今天':shortDate.substr(0,4)+"年"+shortDate.substr(4,2)+"月"+shortDate.substr(6,2)+"日"}}
{
{foodInfo.FoodName}}
{
{foodInfo.Calory}}千卡/{
{foodInfo.Weight}}{
{foodInfo.UnitName}}
{
{(weight_initParams.count*(weight_initParams.minScale)).toFixed(1)}}
{
{activeUnitInfo.UnitName}}
约{
{((activeUnitInfo.Calory/activeUnitInfo.Amount)*(weight_initParams.count*(weight_initParams.minScale))).toFixed(1)}}千卡 {
{((activeUnitInfo.Weight/activeUnitInfo.Amount)*(weight_initParams.count*(weight_initParams.minScale))).toFixed(1)}}{
{foodInfo.UnitName}}
{
{$item.UnitName}}

Css:

.cover{
width:100%;height: 100%;background-color: #000000;position: fixed;top: 0px;left: 0px;opacity: 0.7}.Choice_count{
width: 100%;height:840px;position: fixed;bottom: 0;left: 0;background-color: #ffffff;flex-direction: column;border-top-left-radius:20px;border-top-right-radius:20px}.date_title{
width:100%;height:120px;border-bottom: 1px solid #EAE5DD;align-items: center;} .dateText{
font-size:36px;color: #473B1D;position:absolute;} .date{
width:100%;height: 120px;position: relative;align-items: center;justify-content: center;} .foodUnit{
width:140px;align-items: center;justify-content: center;} .foodUnit text{
lines:1;text-overflow:ellipsis;} .button{
background-color: #FFD359;width:100%;height: 105px;font-size: 32px;color: #473B1D;font-weight: bold;position:absolute;bottom: 0;left: 0;} .unitList{
align-items:center;justify-content: center;} .unit{
padding:10px,0;color:#473b1d;font-size: 32px;} .unitActive{
padding:10px,0;color:#ffd359;font-weight:bold;font-size: 32px;} .amount_module text{
color:#473b1d;font-weight:700} .amount_module{
width:100%;justify-content:center;align-items:flex-end;font-family:'Microsoft YaHei';} .rule_module{
width:100%;background-color:#fff;flex-direction:column;align-items: center;} .amount{
font-size:72px;font-weight:700;position:relative;top:12px;margin-right:6px} .ruler{
margin-top:6px} .new-canvas{
margin-top:20px;height:200px;width:1000px} .food_detail{
align-items: center;height:160px;justify-content:space-between;} .images{
height: 120px;;position: absolute;right:20px;padding:20px;align-items: center} .images image{
height:34px}

script:

代码中

img.src = ‘…/Common/ruler.png’ //加载本地图片
cxt.drawImage(img, (count * -24) + (i * 240), 0); //绘制图片
图片如下:

在这里插入图片描述

在刚开始实现尺子效果时,我并没有用到图片来画,是用canvas的画笔 line 线条来画的尺子,每滑动一次,就用线条绘制整个尺子效果,很耗性能,画面非常卡顿。后来再做优化的时候,尝试通过借助图片来画,可以流畅运行。

这里贴上的代码,是我从项目里摘出来实现尺子效果的核心代码,并不能直接运行。仅供一个实现思路和流程。

转载地址:http://bomen.baihongyu.com/

你可能感兴趣的文章
[LeetCode By Python]121. Best Time to Buy and Sell Stock
查看>>
Android/Linux 内存监视
查看>>
Android2.1消息应用(Messaging)源码学习笔记
查看>>
剑指offer算法题分析与整理(三)
查看>>
JVM并发机制探讨—内存模型、内存可见性和指令重排序
查看>>
nginx+tomcat+memcached (msm)实现 session同步复制
查看>>
WAV文件解析
查看>>
WPF中PATH使用AI导出SVG的方法
查看>>
QT打开项目提示no valid settings file could be found
查看>>
android 代码实现圆角
查看>>
java LinkedList与ArrayList迭代器遍历和for遍历对比
查看>>
drat中构造方法
查看>>
JavaScript的一些基础-数据类型
查看>>
coursesa课程 Python 3 programming 统计文件有多少单词
查看>>
coursesa课程 Python 3 programming course_2_assessment_7 多参数函数练习题
查看>>
coursesa课程 Python 3 programming course_2_assessment_8 sorted练习题
查看>>
多线程使用随机函数需要注意的一点
查看>>
getpeername,getsockname
查看>>
Visual Studio 2010:C++0x新特性
查看>>
所谓的进步和提升,就是完成认知升级
查看>>